--------------------------------------------------------------------------------------------------------
      name:  <unnamed>
       log:  code_03_Stata_Creating_data_JoP_Anom.log
  log type:  text
 opened on:   8 Nov 2022, 23:47:39

. do "code_03_Stata_Creating_data_JoP_Anom.do"

. *** Creates
. * "data_YLE_wide.dta" (extended dataset)
. * "data_YLE_main.dta" (long dataset)
. *** from 
. * "data_src_YLE_merged_wide_anom.dta" (wide merged YLE data)
. * "data_src_munidata_12_17.dta"
. 
. * CHANGE THIS
. *cd /path/to/directory
. cd "02_appendix_replication"
02_appendix_replication

. 
. clear

. 
. use "data_src_YLE_merged_wide_anom.dta"

. 
. gen firstdiff = refugees2017 - refugees2012
(54,202 missing values generated)

. 
. gen loser = 0

. replace loser = 1 if elected2012 == 0
(17,298 real changes made)

. 
. gen spare = 0

. replace spare = 1 if elected2012 == 0.5
(9,080 real changes made)

. 
. gen incumbent = 0

. replace incumbent = 1 if elected2012 == 1
(9,400 real changes made)

. 
. gen rantwice = 0

. replace rantwice = 1 if (!missing(votes2012) & !missing(votes2017))
(9,425 real changes made)

. 
. gen answeredtwice = 0

. replace answeredtwice = 1 if (!missing(refugees2012) & !missing(refugees2017))
(5,763 real changes made)

. 
. *** before reshaping save this for future use: 
. 
. save "data_YLE_wide.dta", replace
file data_YLE_wide.dta saved

. 
. reshape long votes voteshare age elected environment refugees urban population ///    , i(seqnum) j(year) string
(j = 2012 2017)

Data                               Wide   ->   Long
-----------------------------------------------------------------------------
Number of observations           59,965   ->   119,930     
Number of variables                  58   ->   51          
j variable (2 values)                     ->   year
xij variables:
                    votes2012 votes2017   ->   votes
            voteshare2012 voteshare2017   ->   voteshare
                        age2012 age2017   ->   age
                elected2012 elected2017   ->   elected
        environment2012 environment2017   ->   environment
              refugees2012 refugees2017   ->   refugees
                    urban2012 urban2017   ->   urban
          population2012 population2017   ->   population
-----------------------------------------------------------------------------

. 
. *** merging with covariates ***
. 
. destring year, replace
year: all characters numeric; replaced as int

. 
. merge m:1 muni year using "data_src_munidata_12_17.dta", force

    Result                      Number of obs
    -----------------------------------------
    Not matched                             0
    Matched                           119,930  (_merge==3)
    -----------------------------------------

. 
. *** drop not used ones: 
. 
. drop betwee_15_64 over_64 birth_surplus nro_of_families nro_households ///    nro_households nro_private_houses nro_rentals secondary_educ employment ///    non_commuters share_of_pensioners nro_of_jobs agriculture industry service ///    culture_educ social_health _merge

. 
. rename share_of_foreigners foreigners

. rename higher_educ highereduc

. 
. rename seqnum CandidateID

. 
. replace firstdiff = . if year == 2012
(5,763 real changes made, 5,763 to missing)

. 
. gen treat = 0

. 
. replace treat = 1 if (year == 2017 & Treated2015 == 1)
(23,413 real changes made)

. 
. replace incumbent = 0 if year == 2012
(9,400 real changes made)

. replace spare     = 0 if year == 2012
(9,080 real changes made)

. replace loser     = 0 if year == 2012
(17,298 real changes made)

. 
. save "data_YLE_main.dta", replace
file data_YLE_main.dta saved

. 
. *******************************************************************************
. 
end of do-file

. log close
      name:  <unnamed>
       log:  code_03_Stata_Creating_data_JoP_Anom.log
  log type:  text
 closed on:   8 Nov 2022, 23:47:55
--------------------------------------------------------------------------------------------------------
